package com.situ.personal.controller;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.personal.service.DepartmentService;
import com.situ.personal.service.UserService;
import com.situ.personal.util.MD5Util;
import com.situ.personal.entity.Department;
import com.situ.personal.entity.PageInfo;
import com.situ.personal.entity.User;

/**
 * Servlet implementation class UserServlet
 */
@WebServlet("/user")
public class UserServlet extends HttpServlet {
	private UserService userService = new UserService();
	private DepartmentService departmentService = new DepartmentService();
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		 String method = req.getParameter("method");
		if(method == null || "".equals(method)) {
			method = "findByPage";
		}
		
		switch (method) {
		case "findByPage":
			findByPage(req, resp);
			break;
		case "findAll":
			findAll(req, resp);
			break;
		case "deleteById":
			deleteById(req, resp);
			break;
		case "insert":
			insert(req, resp);
			break;
		case "selectById":
			selectById(req, resp);
			break;
		case "update":
			update(req, resp);
			break;
		case "getUserInsertPage":
			getUserInsertPage(req, resp);
			break;
		default:
			break;
		}
	}

	private void getUserInsertPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		List<Department> list = new ArrayList<Department>();
//		Department department1 = new Department(1, "软件1部");
//		Department department2 = new Department(2, "软件2部");
//		Department department3 = new Department(3, "硬件部");
//		list.add(department1);
//		list.add(department2);
//		list.add(department3);
		
		List<Department> list = departmentService.findAll();
		
		req.setAttribute("list", list);
		req.getRequestDispatcher("/user_insert.jsp").forward(req, resp);
		
	}

	private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String pageNo = req.getParameter("pageNo");
		String pageSize = req.getParameter("pageSize");
		// 如果pageNo和pageSize没有传递，设置去pageNo=1,pageSize=5
		if (pageNo == null || "".equals(pageNo)) {
			pageNo = "1";
		}
		if (pageSize == null || "".equals(pageSize)) {
			pageSize = "5";
		}
		//传给service
		PageInfo pageInfo = userService.findByPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
		req.setAttribute("pageInfo",pageInfo);
		req.getRequestDispatcher("/user_page_list.jsp").forward(req, resp);
		
	}

	private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		System.out.println(id);
		String name = req.getParameter("name");
		System.out.println(name);
		String age = req.getParameter("age");
		System.out.println(age);
		String password = req.getParameter("password");
		System.out.println(password);
		String email = req.getParameter("email");
		System.out.println(email);;
		String departmentId = req.getParameter("departmentId");
		System.out.println(departmentId);
		User user = new User(Integer.parseInt(id), name, Integer.parseInt(age), password, email, Integer.parseInt(departmentId));
		System.out.println(user);
		
		boolean isSuccess = userService.update(user);
		
		if (isSuccess) {
			// 修改完之后，重定向
			resp.sendRedirect(req.getContextPath() + "/user?method=findByPage");
		} else {
			resp.sendRedirect(req.getContextPath() + "/error.html");
		}
	}

	private void selectById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String selectId = req.getParameter("id");
		
		User user = userService.selectById(Integer.parseInt(selectId));
		
		req.setAttribute("user",user);
		req.getRequestDispatcher("/user_update.jsp").forward(req, resp);
		
	}

	private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
	
		String name = req.getParameter("name");
		String age = req.getParameter("age");
		String password = req.getParameter("password");
		String email = req.getParameter("email");
		String departmentId = req.getParameter("departmentId");
		System.out.println("错在");
	
		System.out.println(name);
		System.out.println(age);
		System.out.println(password);
		System.out.println(email);
		System.out.println(departmentId);                                
		User user = new User(name, Integer.parseInt(age), password, email, Integer.parseInt(departmentId));
		System.out.println(user);
		
		boolean isSuccess = userService.insert(user);
		
	
		
		// 添加完之后再到查询并展示所有的界面
		// http://localhost:8080/Java1912Web/findAllStudent
		resp.sendRedirect(req.getContextPath() + "/user?method=findByPage");
		
	}

	private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		System.out.println("id:" + id);
		boolean isSuccess = userService.deleteById(Integer.parseInt(id));
		resp.sendRedirect(req.getContextPath() + "/user?method=findByPage");
		
	}

	private void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<User> list = userService.findAll();
		for (User user : list) {
			System.out.println(user);
		}
		req.setAttribute("list", list);
		req.getRequestDispatcher("/user_list.jsp").forward(req, resp);
		
	}
	
	
	public static void main(String[] args) {
		String str = "9";
		String s =  MD5Util.EncodeUtf8(str);
		System.out.println(s);
	}

}
